//
//  SYS_OrderCell.m
//  System_App
//
//  Created by 吴健 on 2017/11/29.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_OrderCell.h"
#import "SYS_OrderListCell.h"

@interface SYS_OrderCell()<UITableViewDelegate, UITableViewDataSource>
{
    SYS_TableView *tbv;
    IBOutlet UIView      *v_center;
    IBOutlet UIImageView *imv_head;
    IBOutlet UILabel     *lb_goodType;
    IBOutlet UILabel     *lb_orderNumber;
    IBOutlet UILabel     *lb_total;
    IBOutlet UILabel     *lb_freight;
    IBOutlet NSLayoutConstraint *lc_centerViewHeight;
    
}
@end

@implementation SYS_OrderCell

+ (SYS_OrderCell *)getOrderCell{
    SYS_OrderCell *cell = nil;
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SYS_OrderCell" owner:self options:nil];
    for (NSObject *obj in objects) {
        if ([obj isKindOfClass:[SYS_OrderCell class]]) {
            cell = (SYS_OrderCell *)obj;
            break;
        }
    }
    return cell;
}

// iOS 11的 margins
- (UIEdgeInsets)layoutMargins {
    [super layoutMargins];
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)setModel:(SYS_OrderListModel *)model{
    _model = model;
    if ([_model.orderSubType isEqualToString:@"transfer"]) {
        lb_goodType.text = @"转移商品";
    }else{
        lb_goodType.text = @"平台商品";
    }
    lb_orderNumber.text = FORMAT(@"订单编号：%@", _model.orderNumber);
    NSInteger allCount = 0;
    //优惠金额
    float couponPrice = 0;
    //实收金额
    float realityPrice = 0;
    for (int i = 0; i < _model.orderItems.count; i++) {
        OrderItems *model = _model.orderItems[i];
        allCount+= model.count.integerValue;
        couponPrice+= [model.discountPrice floatValue];
    }
    realityPrice = _model.total.floatValue - couponPrice;
    lb_total.text = FORMAT(@"共%ld件商品 合计:￥%.2f",allCount, realityPrice);
    lb_freight.text = FORMAT(@"(含运费:￥%.2f)", _model.freight.floatValue);
    
    NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:lb_total.text];
    [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(lb_total.text.length - [FORMAT(@"￥%.2f", realityPrice) length], [FORMAT(@"￥%.2f", realityPrice) length])];
    [attributedText setAttributes:@{NSForegroundColorAttributeName :DSCOLOR_RED} range:NSMakeRange(lb_total.text.length - [FORMAT(@"￥%.2f", realityPrice) length], [FORMAT(@"￥%.2f", realityPrice) length])];
    lb_total.attributedText = attributedText;
    [self setUI];
}

#pragma mark -- setter and getter
- (void)setUI{
    tbv = [[SYS_TableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100*_model.orderItems.count) style:UITableViewStylePlain];
    tbv.separatorStyle = UITableViewCellSeparatorStyleNone;
    tbv.showsHorizontalScrollIndicator = NO;
    tbv.showsVerticalScrollIndicator   = NO;
    tbv.userInteractionEnabled = NO;
    tbv.delegate = self;
    tbv.dataSource = self;
    tbv.backgroundColor = DSCOLOR_WHITE;
    tbv.backgroundView.backgroundColor = DSCOLOR_WHITE;
    [v_center addSubview:tbv];
    
    lc_centerViewHeight.constant = 100*_model.orderItems.count;
    [self updateConstraintsIfNeeded];
    
    self.btn_shipments.layer.masksToBounds = YES;
    self.btn_shipments.layer.cornerRadius  = 12.0f;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:indexPath];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.orderItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"SYS_OrderListCell";
    SYS_OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [SYS_OrderListCell getOrderListCell];
    }
    OrderItems *itemModel = _model.orderItems[indexPath.row];
    cell.itemModel = itemModel;
    return cell;
}
@end
